/** @file    DialogLogin.cpp
 *  @author  Alessandro Polo
 *  @version $Id: DialogLogin.cpp 1708 2010-01-26 22:28:01Z alex $
 *  @brief
 * File containing methods for the wosh::gui::DialogLogin class.
 * The header for this class can be found in DialogLogin.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "DialogLogin.h"
 #include "ui_DialogLogin.h"

 #include <QtGui/QMessageBox>
 #include <core/UserManager.h>
 #include <framework/gui/GuiCache.h>
 #include <framework/security/SecurityAuthenticationPassWord.h>
 #include <framework/security/SecurityAuthenticationOS.h>


using namespace std;
using namespace wosh::security;
namespace wosh {
 namespace gui {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DialogLogin::DialogLogin(QWidget *parent) : QDialog(parent)
 {
	this->m_ui = new Ui::DialogLogin();
	this->m_ui->setupUi(this);

	this->security = NULL;

	QObject::connect(this->m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	QObject::connect(this->m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
	
	setUserName( GuiCache::currentUser() );

	if ( !this->m_ui->txtOS_WoshUserName->text().isEmpty() )
		this->m_ui->tabWidget->setCurrentIndex(1);
 }

DialogLogin::~DialogLogin() {
	delete this->m_ui;
	if ( this->security != NULL )
		delete this->security;
 }

void DialogLogin::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void DialogLogin::setUserName( const std::string& value ) {
	this->m_ui->txtPWD_UserName->setText( QString(value.c_str()) );
	string os_user, wosh_user;
	if ( WSUCCEEDED(SecurityAuthenticationOS::findLoggedUser(os_user, wosh_user)) ) {
		this->m_ui->txtOS_OsUserName->setText( QString(os_user.c_str()) );
		this->m_ui->txtOS_WoshUserName->setText( QString(wosh_user.c_str()) );
	 }
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void DialogLogin::accept() {
	switch( this->m_ui->tabWidget->currentIndex() ) {
		case 0: {
				this->username = this->m_ui->txtPWD_UserName->text().toStdString();
				this->security = new SecurityAuthenticationPassWord( this->m_ui->txtPWD_Password->text().toStdString() );
				break;
			 }
		case 1: {
				this->username = this->m_ui->txtOS_OsUserName->text().toStdString();
				this->security = new SecurityAuthenticationOS();
				break;
			 }
		default: {
				this->username = "";
				this->security = new SecurityAuthentication();
				break;
			 }
	 }
	QDialog::accept();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT DialogLogin::waitValidUser( std::string& username, wosh::SecurityAuthentication* securityAuth ) {

	DialogLogin* dSuser = new DialogLogin();
	int ret = QDialog::Accepted;
	while ( ( ret = dSuser->exec() ) == QDialog::Accepted ) {
		WRESULT auth = UserManager::isAuthorized( dSuser->getUserName(), *dSuser->getSecurityAuthentication() );
		if ( WSUCCEEDED(auth) )
			break;
	 }
	if ( ret == QDialog::Accepted ) {
		username = dSuser->getUserName();
		//securityAuth = dSuser->getSecurityAuthentication();
		return WRET_OK;
	 }
	delete dSuser;
	return WRET_ERR_SECURITY;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
